package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

//@Cmd(code = GameProtocol.S_PARTY_JOIN, desc = "")
@Cmd(code = Protocol.S_BANQUET_SERVER_JOIN, desc = "加入宴会")
public class JoinPartyCmd extends AbstractCommand {
    private String banquetId;
    private int giftId;
    private int type;
    private boolean needSend;
    private int giftAcceptState;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.JoinBanquetReq reqMsg = BanquetProto.JoinBanquetReq.parseFrom(packet.getBytes());
        banquetId = reqMsg.getBanquetId();
        giftId = reqMsg.getGiftId();
        type = reqMsg.getType();
        giftAcceptState = reqMsg.getGiftAcceptState();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
   //     type = 10001;
        BanquetGiftInfo banquetGiftInfo = BanquetConfigMgr.getBanquetGiftInfo(type, giftId);
        String consume = "";
        if (!"0".equals(banquetGiftInfo.getJoinConsume())) {
            consume = banquetGiftInfo.getJoinConsume();
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(banquetGiftInfo.getJoinConsume()))) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                needSend = true;
                return;
            }
        }
        // 预扣除一下
        if (!StringUtils.isNullOrEmpty(consume)) {
            player.getModule(PreReduceModule.class).preReduce(banquetGiftInfo.getJoinConsume());
        }
        BanquetProto.CrossJoinPartyReqMsg.Builder msg = BanquetProto.CrossJoinPartyReqMsg.newBuilder();
        msg.setPartyId(banquetId);
        msg.setGiftId(giftId);
        msg.setConsume(banquetGiftInfo.getJoinConsume());
        msg.setGiftAcceptState(giftAcceptState);
        msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        msg.setPopularity(BanquetMgr.getJoinPopularity(player, type, giftId));
        msg.setIntegral(BanquetMgr.getJoinIntegral(player, type, giftId));
        player.sendPacket(CrossProtocol.C_CROSS_PARTY_JOIN, msg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (needSend) {
            BanquetProto.JoinBanquetResp.Builder respMsg = BanquetProto.JoinBanquetResp.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BANQUET_SERVER_JOIN, respMsg);
        }
    }
}
